package com.example.controller;

import com.example.model.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 * 
 * @author demo
 * @since 1.0.0
 */
@RestController
@RequestMapping("/api/users")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    /**
     * 获取所有用户
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllUsers() {
        List<User> users = userService.getAllUsers();
        Map<String, Object> response = new HashMap<>();
        response.put("data", users);
        response.put("total", users.size());
        response.put("status", "success");
        return ResponseEntity.ok(response);
    }
    
    /**
     * 根据ID获取用户
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getUserById(@PathVariable Long id) {
        User user = userService.getUserById(id);
        Map<String, Object> response = new HashMap<>();
        
        if (user != null) {
            response.put("data", user);
            response.put("status", "success");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "用户不存在");
            response.put("status", "error");
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * 创建新用户
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createUser(@RequestParam String username, 
                                                         @RequestParam String email) {
        User user = userService.createUser(username, email);
        Map<String, Object> response = new HashMap<>();
        response.put("data", user);
        response.put("message", "用户创建成功");
        response.put("status", "success");
        return ResponseEntity.ok(response);
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateUser(@PathVariable Long id,
                                                         @RequestParam String username,
                                                         @RequestParam String email) {
        User user = userService.updateUser(id, username, email);
        Map<String, Object> response = new HashMap<>();
        
        if (user != null) {
            response.put("data", user);
            response.put("message", "用户更新成功");
            response.put("status", "success");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "用户不存在");
            response.put("status", "error");
            return ResponseEntity.notFound().build();
        }
    }
    
    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteUser(@PathVariable Long id) {
        boolean deleted = userService.deleteUser(id);
        Map<String, Object> response = new HashMap<>();
        
        if (deleted) {
            response.put("message", "用户删除成功");
            response.put("status", "success");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "用户不存在");
            response.put("status", "error");
            return ResponseEntity.notFound().build();
        }
    }
} 